/*
 * Copyright 2011 group coding kata nantes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.coding.kata.nantes.bowling.christophe;

/**
 * Frame is composed of two roll or three roll for the last roll.
 */
public class Frame {

    private static final int SCORE_MAX_FOR_ROLL = 10;
    protected int frameNumber = 1;

    Integer theFirstRoll;
    Integer theSecondRoll;
    Integer theLastRoll;

    Frame nextFrame;

    /**
     * calculate the score with all next frame.
     *
     * @return the score.
     */
    int calculateScore() {
        int frameScore = calculateScoreWithoutBonus();
        int nextFrameScore = 0;
        if (nextFrame != null) {
            nextFrameScore = nextFrame.calculateScore();
            if (theFirstRoll == SCORE_MAX_FOR_ROLL) {
                frameScore += calculateScoreTwoNextRoll();

            } else if (frameScore == SCORE_MAX_FOR_ROLL) {
                frameScore += calculateScoreNextRoll();
            }
        }
        return frameScore + nextFrameScore;
    }

    private int calculateScoreNextRoll() {
        return nextFrame.theFirstRoll;
    }

    private int calculateScoreTwoNextRoll() {
        int roll1 = calculateScoreNextRoll();
        int roll2 = 0;
        if (nextFrame.theSecondRoll != null) {
            roll2 = nextFrame.theSecondRoll;
        } else {
            if (nextFrame.nextFrame != null) {
                roll2 = nextFrame.nextFrame.theFirstRoll;
            }
        }
        return roll1 + roll2;
    }

    private int calculateScoreWithoutBonus() {
        int score = theFirstRoll;
        if (theSecondRoll != null) {
            score += theSecondRoll;
            if (theLastRoll != null) {
                score += theLastRoll;
            }
        }
        return score;
    }

    public Frame addStrike() {
        addPinDown(SCORE_MAX_FOR_ROLL);
        return endOfFrame();
    }

    public Frame addSpare() {
        if (theSecondRoll == null) {
            return addPinDown(SCORE_MAX_FOR_ROLL - theFirstRoll);
        } else {
            return addPinDown(SCORE_MAX_FOR_ROLL - theSecondRoll);
        }
    }

    public Frame addMiss() {
        return addPinDown(0);
    }

    public Frame addPinDown(int nbPinDown) {
        if (theFirstRoll == null) {
            theFirstRoll = nbPinDown;
            return this;
        } else if (theSecondRoll == null) {
            theSecondRoll = nbPinDown;
            return endOfFrame();
        } else {
            theLastRoll = nbPinDown;
            return this;
        }
    }

    private Frame endOfFrame() {
        if (frameNumber < SCORE_MAX_FOR_ROLL) {
            nextFrame = new Frame();
            nextFrame.frameNumber = frameNumber + 1;
            return nextFrame;
        } else {
            return this;
        }

    }

}
